package pw.yumc.Residence.commandsub;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ResidenceManager;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.selection.SelectionManager;
import pw.yumc.Residence.text.Language;

public class CommandCreate extends BaseCommand {
    ResidenceMain plugin;

    public CommandCreate(final ResidenceMain plugin) {
        super("create", "new", "c");
        this.plugin = plugin;
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
        setPermission("residence.create");
        setPossibleArguments("<领地名称>");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final Player player = (Player) sender;
        final boolean resadmin = (command != null);
        if (plugin.getDeny().getStringList("world").contains(player.getWorld().getName()) && !resadmin && !player.isOp()) {
            player.sendMessage("§c当前世界不允许圈地!");
            return;
        }
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();
        final SelectionManager smanager = plugin.getSelectionManager();
        final WorldEditPlugin wep = (WorldEditPlugin) plugin.getServer().getPluginManager().getPlugin("WorldEdit");
        if (wep != null) {
            if (wep.getConfig().getInt("wand-item") == plugin.getConfigManager().getSelectionTooldID()) {
                smanager.worldEdit(player);
            }
        }
        if (smanager.hasPlacedBoth(player.getName())) {
            rmanager.addResidence(player, args[0], smanager.getPlayerLoc1(player.getName()), smanager.getPlayerLoc2(player.getName()), resadmin);
        } else {
            player.sendMessage(ChatColor.RED + language.getPhrase("SelectPoints"));
        }
    }

}
